<?php
include $_SERVER['DOCUMENT_ROOT'].'/configs/define.php';
ini_set('display_errors',0);
error_reporting(15);

$connect=mysql_connect('localhost','root','') or die('Khong the ket noi');
mysql_select_db('sw_muabanxe',$connect);

$query = "SELECT id, image, image1, image2 FROM motor where cat_id=0 limit 20";
$rs=mysql_query($query);

echo "Cac image can chuyen doi: <br/>";
while($data=mysql_fetch_array($rs)){
	echo 'id:'.$data['id'].'<br/>';
	if(null != $data['image'] && strpos($data['image'], '/') === false) {
		echo '==>'.$data['image'].' <br/>';
		updateImage($data['id'],changeDir(UPLOAD,$data['image']),1);
	}
	if(null != $data['image1'] && strpos($data['image1'], '/') === false) {
		echo '==>'.$data['image1'].' <br/>';
		updateImage($data['id'],changeDir(UPLOAD,$data['image1']),2);
	}
	if(null != $data['image2'] && strpos($data['image2'], '/') === false) {
		echo '==>'.$data['image2'].' <br/>';
		updateImage($data['id'],changeDir(UPLOAD,$data['image2']),3);
	}
	$query = "UPDATE motor SET cat_id=1 WHERE id = ".$data['id'];
	mysql_query($query);
}

function updateImage($id, $img, $case) {
	switch ($case) {
		case 1:
			$query = "UPDATE motor SET image='".$img."' WHERE id = ".$id;
			mysql_query($query);
			break;
		case 2:
			$query = "UPDATE motor SET image1 = '".$img."' WHERE id = ".$id;
			mysql_query($query);
			break;
		case 3:
			$query = "UPDATE motor SET image2='".$img."' WHERE id = ".$id;
			mysql_query($query);
			break;
		default:
			echo "Khong co ngoai le";
	}

}


function changeDir($path,$name) {

	$name_file = substr(strtolower($name),0,strlen($name)-4);
	$ext = substr(strtolower($name),-3);
	if($ext == "gif" || $ext == "jpg" || $ext == "png"){
		$thumb = make_thumb($name,$path.$name,'default','true');
		$arrDate = getdate();
		return $arrDate['year'].'/8/'.$name;
	} else {
		return $name;
	}
}


function make_thumb($img_name,$path,$type='default',$datefolder='true',$fpath='')
{

	$site = array();
	$site['item']=array('width'=>77,'height'=>61);
	$site['detail']=array('width'=>300,'height'=>250);
	$site['full']=array('width'=>800,'height'=>600);

	$ext=getExtension($img_name);
	//creates the new image using the appropriate function from gd library

	foreach($site as $key => $tmpBlog)
	{
		if(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext))
		$src_img=imagecreatefromjpeg($path);

		if(!strcmp("png",$ext))
		$src_img=imagecreatefrompng($path);

		if(!strcmp("gif",$ext))
		$src_img=imagecreatefromgif($path);


		//gets the dimmensions of the image
		$old_x=imageSX($src_img);
		$old_y=imageSY($src_img);
		$new_w = $tmpBlog['width'];
		$new_h=$tmpBlog['height'];

		$ratio1=$old_x/$new_w;
		$ratio2=$old_y/$new_h;
		if($ratio1>$ratio2 && $ratio1 > 1) {
			if($new_w < $old_x) {
				$thumb_w=$new_w;
				$thumb_h=$old_y/$ratio1;
			} else {
				$thumb_w = $old_x;
				$thumb_h=$old_y/$ratio1;
			}
		} else {
			if($new_h < $old_y) {
				$thumb_w=$new_w;
				$thumb_h=$old_y/$ratio1;
			} else {
				$thumb_w = $old_x;
				$thumb_h = $old_y;
			}
		}



		// we create a new image with the new dimmensions
		$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

		// resize the big image to the new created one
		imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
		if($datefolder=='true')
		{
			$arrDate = getdate();
			$dirPathYear = UPLOAD.$fpath.$type.'/'.$key.'/'.$arrDate['year'];
			$dirPathMonth = $dirPathYear.'/8/';
			//'.$arrDate['mon'].'
			if(!is_dir($dirPathMonth)){
				mkdir($dirPathMonth,0777,true);
			}
			$filename=$dirPathMonth.$img_name;
		}
		else
		{
			$filename=UPLOAD.$fpath.$type.'/'.$key.'/'.$img_name;
		}

			
		// output the created image to the file. Now we will have the thumbnail into the file named by $filename
		if(!strcmp("png",$ext))
		imagepng($dst_img,$filename);
		else if(!strcmp("jpg",$ext)||!strcmp("jpeg",$ext))
		imagejpeg($dst_img,$filename);
		else if(!strcmp("gif",$ext))
		imagegif($dst_img,$filename);

		//destroys source and destination images.
		imagedestroy($dst_img);
		imagedestroy($src_img);
	}
}

function getExtension($str)
{
	$i = strrpos($str,".");
	if (!$i) { return ""; }
	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l);
	return $ext;
}

die();
foreach(glob($_SERVER['DOCUMENT_ROOT'].'/static/upload/default/item/2011/8/*') as $file) {
   if(is_file($file)) {
      unlink($file);
   }
}
die();
?>